﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.kiosk {
    import reprise.ui.*;
    import flash.events.*;
    import flash.display.*;
    import reprise.controls.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.fountain.*;

    public class NeedCraftItemMediator extends Mediator {

        private var translations:Dictionary;
        private var hasNeeded:Boolean;
        private var solvedIcon:UIComponent;
        private var amountLabel:Label;
        private var collectedCount:int;
        private var orderGoods:LabelButton;
        private var askButton:LabelButton;
        public var symbolModel:SymbolModel;
        public var userModel:UserModel;
        public var language:LanguageModel;
        public var contractsModel:ContractsModel;
        public var sessionModel:SessionModel;

        override public function onRegister():void{
            super.onRegister();
            addContextListener(UserModelEvent.ON_MODEL_CHANGED, this.onModelChanged, UserModelEvent);
            addContextListener(EventConstants.UPDATE_SYMBOLS, this.onSymbolsChanged, InfoEvent);
            this.initTranslations();
            this.createItem();
        }
        private function initTranslations():void{
            this.translations = new Dictionary();
            this.translations[GameConstants.LOVE] = "fountain_symbol_love";
            this.translations[GameConstants.FIRE] = "fountain_symbol_fire";
            this.translations[GameConstants.WATER] = "fountain_symbol_water";
            this.translations[GameConstants.ICE] = "fountain_symbol_ice";
            this.translations[GameConstants.STAR] = "fountain_symbol_star";
            this.translations[GameConstants.AIR] = "fountain_symbol_air";
            this.translations[GameConstants.COINS] = "global_coins";
            this.translations[GameConstants.GOODS.toLowerCase()] = "global_goods";
        }
        private function onModelChanged(_arg1:UserModelEvent):void{
            this.updateItem();
        }
        private function onSymbolsChanged(_arg1:InfoEvent):void{
            this.updateItem();
        }
        private function updateItem():void{
            var _local1:int = this.view.item.amount;
            this.collectedCount = this.getRequiredItemCount(this.view.item);
            if (this.view.item.amount > this.collectedCount){
                this.hasNeeded = false;
                if (((this.solvedIcon) && (this.view.contains(this.solvedIcon)))){
                    this.view.removeChild(this.solvedIcon);
                };
                this.view.removeCSSClass("neededItemGreen");
                this.setItemType();
            } else {
                this.collectedCount = this.view.item.amount;
            };
            this.updateLabelText();
        }
        private function updateLabelText():void{
            var _local1:String;
            if (this.view.item.type == GameConstants.COINS){
                _local1 = this.view.item.amount.toString();
            } else {
                _local1 = this.language.getTextByKey("global_of_new", {has:this.collectedCount, needed:this.view.item.amount});
            };
            this.amountLabel.label = _local1;
        }
        private function createItem():void{
            this.collectedCount = this.getRequiredItemCount(this.view.item);
            this.hasNeeded = (this.collectedCount >= this.view.item.amount);
            if (this.hasNeeded){
                this.view.addCSSClass("neededItemGreen");
            };
            this.addLabel("gemDescription myriadProFontNormal", this.translations[this.view.item.type], null, this.view);
            this.addGemIcon(this.view, this.view.item.type);
            this.setItemType();
        }
        private function setItemType():void{
            var _local1:Object = this.view.item;
            if (this.hasNeeded){
                this.solvedIcon = this.addSolvedIcon(this.view);
                this.collectedCount = _local1.amount;
            } else {
                if (_local1.type == GameConstants.GOODS_ASSETS){
                    if (!this.orderGoods){
                        this.addOrderButton(this.view, _local1.type);
                    };
                } else {
                    if (_local1.type != GameConstants.COINS){
                        if (!this.askButton){
                            this.addAskButton(this.view, _local1.type);
                        };
                    };
                };
            };
            if (!this.amountLabel){
                this.amountLabel = RepriseUtils.addLabel(this.view, "neededLabel myriadProFontOutline", "");
            };
            this.updateLabelText();
        }
        private function getRequiredItemCount(_arg1:Object):int{
            var _local2:int;
            switch (_arg1.type){
                case GameConstants.GOODS_ASSETS:
                    _local2 = this.userModel.goods;
                    break;
                case ProtocolConstants.COINS:
                    _local2 = this.userModel.coins;
                    break;
                default:
                    _local2 = this.symbolModel.symbols[_arg1.type];
                    break;
            };
            return (_local2);
        }
        protected function addAskButton(_arg1:UIComponent, _arg2:String):void{
            if (this.sessionModel.getRequestLimit(_arg2)){
                return;
            };
            this.askButton = this.createButton(_arg1, "kioskButton blueButton", "global_button_ask", "myriadProFont");
            this.askButton.setData(_arg2);
            eventMap.mapListener(this.askButton, MouseEvent.CLICK, this.onAskClick);
        }
        protected function addOrderButton(_arg1:UIComponent, _arg2:String):void{
            var _local4:String;
            var _local5:Function;
            var _local3 = !((this.contractsModel.state == ContractsModel.WAITING));
            switch (this.contractsModel.state){
                case ContractsModel.WAITING:
                    this.addLabel("goodsOrderedLabel myriadProFontNormal", "order_goods_ordered", null, _arg1);
                    break;
                case ContractsModel.DELIVER_GOODS:
                case ContractsModel.READY_TO_FINALIZE:
                    this.orderGoods = this.createButton(_arg1, "orderButton kioskButton blueButton", "order_goods_button", "myriadProFont");
                    _local4 = "global_button_collect";
                    _local5 = this.onCollectClick;
                    break;
                case ContractsModel.READY_TO_CREATE:
                    this.orderGoods = this.createButton(_arg1, "orderButton kioskButton blueButton", "order_goods_button", "myriadProFont");
                    _local4 = "order_goods_button";
                    _local5 = this.onOrderClick;
                    break;
                default:
                    break;
            };
            if (this.orderGoods){
                this.orderGoods.enabled = _local3;
                this.orderGoods.setData(_arg2);
                this.orderGoods.setLabel(this.language.getTextByKey(_local4));
                eventMap.mapListener(this.orderGoods, MouseEvent.CLICK, _local5);
            };
        }
        protected function onAskClick(_arg1:MouseEvent):void{
            var _local2:String = _arg1.currentTarget.data();
            var _local3:InfoEvent = new InfoEvent(GameConstants.ASK_FOR_SYMBOL, _local2);
            dispatch(_local3);
        }
        protected function closePopup():void{
            var _local1:InfoEvent = new InfoEvent(EventConstants.CLOSE_POPUP_BY_TYPE, GameConstants.KIOSK_POPUP);
            dispatch(_local1);
        }
        private function centerVan():void{
            var _local1 = "5";
            dispatch(new InfoEvent(EventConstants.SHOW_ROOM, _local1));
        }
        protected function onOrderClick(_arg1:MouseEvent):void{
            this.closePopup();
            this.centerVan();
            dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.GOODS_POPUP));
        }
        private function onCollectClick(_arg1:MouseEvent):void{
            this.closePopup();
            this.centerVan();
        }
        protected function addSolvedIcon(_arg1:UIComponent):UIComponent{
            var target = _arg1;
            var solvedClip:MovieClip = AssetsLibrary.getMovieClip("SolvedIcon");
            var solvedIcon:UIComponent = RepriseUtils.addClipComponent(target, "solvedIcon", solvedClip);
            RepriseUtils.validateComponent(solvedIcon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, -1);
            });
            return (solvedIcon);
        }
        protected function addGemIcon(_arg1:UIComponent, _arg2:String):void{
            var target = _arg1;
            var key = _arg2;
            var iconClip:MovieClip = AssetsLibrary.getMovieClip((("Reward" + Strings.capitalize(key)) + "Thumb"));
            var icon:UIComponent = RepriseUtils.addClipComponent(target, "gemContainer", iconClip);
            RepriseUtils.validateComponent(icon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, 0, 0);
            });
        }
        protected function createButton(_arg1:UIComponent, _arg2:String, _arg3:String, _arg4:String, _arg5:Object=null):LabelButton{
            return (RepriseUtils.addLabelButton(_arg1, _arg2, this.language.getTextByKey(_arg3, _arg5), null, null, _arg4));
        }
        protected function addLabel(_arg1:String, _arg2:String, _arg3:Object=null, _arg4:UIComponent=null, _arg5:String=null):Label{
            var _local6:String = this.language.getTextByKey(_arg2, _arg3);
            return (RepriseUtils.addLabel(((_arg4) || (this.view)), _arg1, _local6, _arg5));
        }
        private function get view():NeededCraftItem{
            return ((viewComponent as NeededCraftItem));
        }

    }
}//package net.wooga.pets.views.popups.kiosk 
